//
//  HomeActivityViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-14.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityDetailViewController.h"
#import "HomeActivityCell.h"

@implementation ActivityListViewController

- (id)init
{
    self = [super initWithNibClass:[ActivityListViewController class]];
    if (self) {
        _dataSource = [[ActivityListSource alloc] initWithVC:self];
        [_dataSource fetchActivitysLoadMore:NO];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

#pragma 隐藏tabbar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self createNavigation:@"活动"];
    
    _activityTable.delegate = _dataSource;
    _activityTable.dataSource = _dataSource;
}

- (void)leftButtonClick:(id)sender
{
    [AppDelegate changeToNormalTabBar];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)appendTableWith:(NSMutableArray *)data
{
    for (int i = 0; i < [data count]; i++) {
        [_dataSource.activityList addObject:[data objectAtIndex:i]];
    }
    
    NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];
    
    for (int ind = 0; ind < [data count]; ind++) {
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:[_dataSource.activityList indexOfObject:[data objectAtIndex:ind]] inSection:0];
        [insertIndexPaths addObject:newPath];
    }
    
    [_activityTable insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    self.moreLoading = NO;
}

@end

//=========================================================================================
#pragma ===================数据源类实现======================================================
//=========================================================================================

@implementation ActivityListSource

#define FETCH_NEW_TAG  1001
#define FETCH_MORE_TAG 1002
#define PAGE_COUNT 5

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
        _activityList = [[NSMutableArray alloc] initWithCapacity:10];
    }
    return self;
}

- (void)fetchActivitysLoadMore:(BOOL)isLoadMore
{
    if (!isLoadMore) [SVProgressHUD show];
    
    self.pageNumber = isLoadMore ? self.pageNumber : 1;
    
    NSString *url = [NSString stringWithFormat:API_HOME_ACTIVITY, self.pageNumber,[AppSetting cityId]];
    [self get:url tag:isLoadMore ? FETCH_MORE_TAG:FETCH_NEW_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSDictionary *json = [request.responseData JSONValue];
    NSDictionary *data = [json objectForKey:@"data"];
    
    if (request.tag == FETCH_NEW_TAG) {
        self.activityList = [data objectForKey:@"huodong"];
        [self.viewController.activityTable reloadData];
    }
    
    if (request.tag == FETCH_MORE_TAG) {
        [self.viewController appendTableWith:[data objectForKey:@"huodong"]];
    }
    
    [SVProgressHUD dismiss];
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _activityList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellId = @"HomeActivityCell";
    
    HomeActivityCell *cell = [_viewController createCellWithNib:CellId tableview:tableView];
    
    NSDictionary *dictionary = [_activityList objectAtIndex:indexPath.row];
    [cell setContent:dictionary];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [_activityList objectAtIndex:indexPath.row];
    BOOL isUse = [[dic objectForKey:@"zhuangtai"] isEqualToString:@"过期"];
    NSString *activityId = [dic objectForKey:@"id"];
    
    ActivityDetailViewController *detailVC = [[ActivityDetailViewController alloc] initWithActivityId:activityId];
    detailVC.isUse = isUse;
    [_viewController.navigationController pushViewController:detailVC animated:YES];
}

#pragma 分页加载
-(void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == _activityList.count-2) {
        if (!self.viewController.moreLoading) {
            self.pageNumber++;
            [self fetchActivitysLoadMore:YES];
            
            self.viewController.moreLoading = YES;
        }
    }
}

@end




